//
//  MineViewController.m
//  Taste
//
//  Created by 徐昊 on 16/8/27.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "HXMacro.h"
#import "LogInViewController.h"
#import "ZWYNetTool.h"
#import "UserInformationRes.h"
#import "UserInfomationModel.h"
#import "UIImageView+WebCache.h"
#import "NSString+Vali.h"

#define kMineCellName @"mineReuse"
@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *mineArray;
@property (nonatomic, strong) UserInfomationModel *model;
@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *userUid = [user objectForKey:@"uid"];
    NSString *userUuid = [user objectForKey:@"uuid"];
    if (userUid) {
        NSString *bodyString = [NSString stringWithFormat:@"notifymaxtime=0&uid=%@&uuid=%@", userUid, userUuid];
        NSString *bodyLength = [NSString stringWithFormat:@"%ld", [bodyString length]];
        NSDictionary *dic = @{@"uuid" : userUuid,
                              @"version" : @"2.50",
                              @"uid" : userUid,
                              @"Content-Type" : @"application/x-www-form-urlencoded",
                              @"Content-Length" : bodyLength};
        [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=user&a=myinfo" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
            _model = [UserInfomationModel modelWithDic:result];
            [_tableView reloadData];
        } andFailureBlock:^(NSError *error) {
            NSLog(@"请求失败");
        }];
    }
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"个人";
    self.tableView = [[UITableView alloc] initWithFrame:SCREEN_RECT];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kMineCellName];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Mine" ofType:@"plist"];
    _mineArray = [NSMutableArray arrayWithContentsOfFile:path];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 90;
    } else {
        return 50;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_mineArray[section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _mineArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineCellName];
    NSString *pic = [_mineArray[indexPath.section][indexPath.row] objectForKey:@"pic"];
    cell.pic.image = [UIImage imageNamed:pic];
    NSString *label = [_mineArray[indexPath.section][indexPath.row] objectForKey:@"label"];
    cell.label.text = label;
    if (indexPath.section == 0) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *userUid = [user objectForKey:@"uid"];
        if (userUid) {
            cell.label.text = _model.res.uname;
            [cell.pic sd_setImageWithURL:[NSURL URLWithString:[NSString imageWebWithStr:_model.res.img_url]] placeholderImage:[UIImage imageNamed:@"IMG_Universal_Placeholder_Avatar"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
                UIGraphicsBeginImageContextWithOptions(cell.pic.bounds.size, NO, 1.0);
                [[UIBezierPath bezierPathWithRoundedRect:cell.pic.bounds cornerRadius:cell.pic.frame.size.width] addClip];
                [cell.pic drawRect:cell.pic.bounds];
                cell.pic.image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
            }];
        }
        // 图片做圆
        UIGraphicsBeginImageContextWithOptions(cell.pic.bounds.size, NO, 1.0);
        [[UIBezierPath bezierPathWithRoundedRect:cell.pic.bounds cornerRadius:cell.pic.frame.size.width] addClip];
        [cell.pic drawRect:cell.pic.bounds];
        cell.pic.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LogInViewController *vc = [[LogInViewController alloc] init];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user removeObjectForKey:@"uid"];
        [user removeObjectForKey:@"uuid"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
